goog.provide('bombernode.Player');

goog.require('lime.Sprite');
goog.require('bombernode.Bomb');

bombernode.Player = function(startX, startY, game) {
    lime.Sprite.call(this);

    this.game = game;
    this.V = 0.4;
    this.vx = 0;
    this.vy = 0;

    this.bombs = 5;
    this.droppingBomb = false;
    this.bombDistance = 2;

    this.setAnchorPoint(0,0);
    this.setSize(this.game.BLOCKWIDTH * 0.5, this.game.BLOCKHEIGHT * 0.5);
    this.setFill('#000');
    this.setPosition(startX, startY);
    this.initMovement();
}
goog.inherits(bombernode.Player, lime.Sprite);

bombernode.Player.prototype.move = function(dt) {
    if(this.vx != 0 || this.vy != 0)
    {

        var newX = this.getPosition().x + this.vx * dt,
            newY = this.getPosition().y + this.vy * dt;
        this.checkCollisionAndSetPosition(newX, this.getPosition().y, this.vx, 0);
        this.checkCollisionAndSetPosition(this.getPosition().x, newY, 0, this.vy);
    }
}

bombernode.Player.prototype.checkCollisionAndSetPosition = function(pxl, pyu, vx, vy) {
    var pxr = pxl + this.getSize().width;
    var pyb = pyu + this.getSize().height;

    if(pxl < 0) pxl = 0;
    else if(pxr > this.game.getSize().width) pxl = this.game.getSize().width - this.getSize().width;

    if(pyu < 0) pyu = 0;
    else if(pyb > this.game.getSize().height) pyu = this.game.getSize().height - this.getSize().height;

    var cellIndexLU = this.game.getCellIndex(pxl, pyu);
    var cellIndexRB = this.game.getCellIndex(pxr, pyb);

    var collidesX = cellIndexLU.x != cellIndexRB.x || cellIndexRB.x % 2 == 1;
    var collidesY = cellIndexLU.y != cellIndexRB.y || cellIndexRB.y % 2 == 1;

    if((collidesX && collidesY))
    {
        if(vx > 0)
            pxl = cellIndexRB.x * this.game.BLOCKWIDTH - this.getSize().width - 1;
        else if(vx < 0)
            pxl = (cellIndexLU.x + 1) * this.game.BLOCKWIDTH + 1;

        if(vy > 0)
            pyu = cellIndexRB.y * this.game.BLOCKHEIGHT - this.getSize().height - 1;
        else if(vy < 0)
            pyu = (cellIndexLU.y + 1) * this.game.BLOCKHEIGHT + 1;
    }

    var oldPos = this.getPosition();
    this.setPosition(pxl, pyu);

    for(e in this.game.blocks)
    {
        var block = this.game.blocks[e];
        if(this.hasCollision(block))
        {
            if(vx != 0) pxl = oldPos.x;
            if(vy != 0) pyu = oldPos.y;
            this.setPosition(pxl, pyu);
            return;
        }
    }
}

bombernode.Player.prototype.dropBomb = function() {
    console.log('foo');
    if(!this.droppingBomb && this.bombs > 0)
    {
        var xPos = this.getPosition().x + this.getSize().width / 2;
        var yPos = this.getPosition().y + this.getSize().height / 2;
        xPos = Math.floor(xPos / this.game.BLOCKWIDTH) * this.game.BLOCKWIDTH + this.game.BLOCKWIDTH / 2;
        yPos = Math.floor(yPos / this.game.BLOCKHEIGHT) * this.game.BLOCKHEIGHT + this.game.BLOCKHEIGHT / 2;

        var arrayPos = this.game.getCellIndex(xPos, yPos);

        console.log(arrayPos, (this.game.bombs[arrayPos.x][arrayPos.y]));

        if(!this.game.bombs[arrayPos.x][arrayPos.y])
        {
            this.droppingBomb = true;
            this.bombs--;

            var bomb = new bombernode.Bomb(this, arrayPos);
            this.game.appendChild(bomb);

            //block dropping for 500 msecs
            lime.scheduleManager.callAfter(function(){this.droppingBomb = false;}, this, 500);
        }


    }
}

bombernode.Player.prototype.initMovement = function() {
    goog.events.listen(this, ['keydown', 'keyup'], function(e) {
        var val = (e.event.type == 'keydown') ? this.V : 0;
        switch(e.event.keyCode)
        {
            case 38: //down
                this.vy = -val;
                break;
            case 40: //up
                this.vy = val;
                break;
            case 39: //right
                this.vx = val;
                break;
            case 37: //left
                this.vx = -val;
                break;
            case 32: //space
                if(e.event.type == 'keydown')
                {
                    this.dropBomb();
                }
                break;
            default:
                //console.log('key', e.event.keyCode);
        }
    }, false, this);
};

bombernode.Player.prototype.hasCollision = function(block)
{
    return goog.math.Box.intersects(this.getBoundingBox(), block.getBoundingBox());
}
